
import {

  submitRefundOrderMoney,
  refundOaOrder,//拒绝退款
  agreeOaOrderRefund,//同意退款
} from "../../../../service/jb_order_service"
import {
  chooseMultipleOSSImgUploadQiniuAction
} from "../../../../service/upload_service"
import {
  SERVICE_URL,
  SERVICE_ID,
} from "../../../../service/base_service"

import debounce from "../../../../tools/debounce"

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSelectPop: false,
    showPopType: "yx",
    showPopTyepTitle: "退款原因",
    requestData: {
      text: "收货地址填错了", //退款选择的原因
      id: null, //订单id
      refund_reason_wap_explain: "",
      refund_reason_wap_img: "",//图片字符串逗号隔开 
      uni: null, //orderId order88429170520210715334424
      refund_method: 0, //0:退货退款  1：仅退款 2：退换货
    },

    reasonImgs: [

    ],//凭证图片
    customArr: [
      {
        value: "收货地址填错了",
        label: "收货地址填错了",
      },
      {
        value: "与描述不符",
        label: "与描述不符",
      },
      {
        value: "信息填错了，重新拍 ",
        label: "信息填错了，重新拍 ",
      },
      {
        value: "未按预定时间发货",
        label: "未按预定时间发货",
      },
      {
        value: "其它原因",
        label: "其它原因",
      }
    ],
    reasonTextArr: [],
    orderInfo: {
      "id": null,
      "orderId": "",
      "paid": true,
      "payPrice": "0.00",
      "totalNum": 1,
      "orderInfoList": [

      ]
    },//订单信息
    showRefundPop: false,
    showAgreePop: false,
    refundMoney: '',//退款金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      orderInfo: app.globalData.refundData,
      refundMoney: app.globalData.refundData.payPrice
    })
    if (app.globalData.refundData.refundReasonWapImg) {
      let imgs = app.globalData.refundData.refundReasonWapImg.split(',')
      this.setData({
        reasonImgs: imgs
      })
    } else {
      this.setData({
        reasonImgs: []
      })
    }


  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  clickCenterPopupOverlay() {
    this.setData({
      showAgreePop: false
    })
  },
  refundMoneyChange(e) {
    this.setData({
      refundMoney: e.detail.value
    })
  },
  sureAgreeReq() {
    if (this.data.refundMoney == '' || parseFloat(this.data.refundMoney) < 0) {
      wx.showToast({
        title: '请输入正确的退款金额',
        icon: "none"
      })
      return;
    }
    wx.showLoading();
    agreeOaOrderRefund(this.data.orderInfo.orderId, this.data.refundMoney).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        this.setData({
          showAgreePop: false,
        })
        wx.showToast({
          title: '操作成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '操作失败',
        icon: "none"
      })
      console.error("JJLog:agreeOaOrderRefund", err);
    })
  },
  agreePopAction() {
    this.setData({
      showAgreePop: true,
    })
  },
  //拒绝申请
  refundAction() {
    this.setData({
      showRefundPop: true,
    })
  },
  refundPopHide() {
    this.setData({
      showRefundPop: false
    })
  },
  //确认拒绝申请
  refundSureAction(e) {
    console.log("JJLog:e:" + JSON.stringify(e));

    let valueText = e.detail.comment;
    wx.showLoading();
    refundOaOrder(this.data.orderInfo.orderId, valueText).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "success"
        })
        this.setData({
          showRefundPop: false
        })
        setTimeout(() => {
          wx.navigateBack();
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:refundOaOrder", err);
    })
  },
  previewImgAction(e) {
    let url = e.currentTarget.dataset.url
    wx.previewImage({
      urls: this.data.reasonImgs,
      current: url,
    })
  },
  applyRefundAction() {
    let pics = []
    if (this.data.reasonImgs && this.data.reasonImgs.length > 0) {
      pics = this.data.reasonImgs.map(i => i.imgUrl);
    }
    if (pics.length > 0) {
      this.setData({
        'requestData.refund_reason_wap_img': pics.join(',')
      })
    }
    console.log("JJLog:退款参数:" + JSON.stringify(this.data.requestData));
    debounce(this.submitRefundOrderMoney)
  },

  // 提交退款申请
  submitRefundOrderMoney() {
    wx.showLoading();
    submitRefundOrderMoney(this.data.requestData).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.redirectTo({
          url: '/featurePackage/pages/commonSuccess/commonSuccess?type=1',
        })
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '申请失败',
        icon: "none"
      })
      console.error("JJLog:submitRefundOrderMoney", err);
    })
  },


  showSelectPopAction() {
    this.setData({
      showSelectPop: true,
    })
  },
  hidePopup() {
    this.setData({
      showSelectPop: false
    })
  },
  //确认选择
  sureReasonSelect(e) {
    console.log("JJLog:确认选择啦:" + JSON.stringify(e));
    this.setData({
      'requestData.text': e.detail.result.value,
      showSelectPop: false
    })
  },
  bindRemarkText(e) {
    this.setData({
      'requestData.refund_reason_wap_explain': e.detail.value
    })
  },
  changeRefundMethod(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      'requestData.refund_method': index,
    })
  },


  //上传图片方法，多张上传哦。
  pushReplyImgAction() {
    let num = 3;
    let imgLength = this.data.reasonImgs.length;
    let arr = this.data.reasonImgs || []
    chooseMultipleOSSImgUploadQiniuAction("reply", num - imgLength).then(res => {
      console.log("JJLog:多张图片上传:" + JSON.stringify(res));
      if (res && res.length > 0) {
        res.forEach(i => {
          arr.push(i)
        })
        this.setData({
          reasonImgs: arr
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },
  openServiceAction() {
    wx.openCustomerServiceChat({
      extInfo: { url: SERVICE_URL },
      corpId: SERVICE_ID,
      success(res) { },
      fail(err) {
        console.log('调用失败:', err);
      }
    })
  },
  cancelOneImg(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.reasonImgs;
    arr.splice(index, 1);
    this.setData({
      reasonImgs: arr
    })
  },


})